﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Orchard;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc.Html;
using Orchard.Utility.Extensions;

namespace PJS.Bootstrap.Providers.Layouts {
    public class LayoutShapes : IDependency
    {
        public LayoutShapes()
        {
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        [Shape]
        public void Carousel(dynamic Display, TextWriter Output, HtmlHelper Html, string Id, IEnumerable<dynamic> Items,
                            IEnumerable<string> OuterClasses, IDictionary<string, string> OuterAttributes,
                            IEnumerable<string> InnerClasses, IDictionary<string, string> InnerAttributes,
                            IEnumerable<string> FirstItemClasses, IDictionary<string, string> FirstItemAttributes, IEnumerable<string> ItemClasses, IDictionary<string, string> ItemAttributes)
        {
            if (Items == null) return;

            var items = Items.ToList();
            var itemsCount = items.Count;

            if (itemsCount < 1) return;

            var outerDivTag = GetTagBuilder("div", Id, OuterClasses, new Dictionary<string, string>() { {"data-interval", "3000"},
  {"data-ride", "carousel"}});

            var innerDivTag = GetTagBuilder("div", string.Empty, InnerClasses, InnerAttributes);
            var firstItemTag = GetTagBuilder("div", string.Empty, FirstItemClasses, FirstItemAttributes);
            var itemTag = GetTagBuilder("div", string.Empty, ItemClasses, ItemAttributes);

            var olTag = GetTagBuilder("ol", string.Empty, new List<string>(){"carousel-indicators"}, null);
            Output.Write(outerDivTag.ToString(TagRenderMode.StartTag));
            
            Output.Write(olTag.ToString(TagRenderMode.StartTag));

            for (int j =0; j < items.Count(); j++ )
            {
                TagBuilder liTag = GetTagBuilder("li", string.Empty, j == 0 ? new List<string>() {"slide-one active"} : new List<string>() { "slide-two" }, null);

                Output.Write(liTag.ToString(TagRenderMode.StartTag));
                Output.Write(liTag.ToString(TagRenderMode.EndTag));
            }
            Output.Write(olTag.ToString(TagRenderMode.EndTag));

            Output.Write(innerDivTag.ToString(TagRenderMode.StartTag));

            int i = 0;

            foreach (var item in items)
            {
                if (i == 0)
                    Output.Write(firstItemTag.ToString(TagRenderMode.StartTag));
                else
                    Output.Write(itemTag.ToString(TagRenderMode.StartTag));

                Output.Write(Display(item));
                Output.Write(itemTag.ToString(TagRenderMode.EndTag));
                i++;
            }

            Output.Write(innerDivTag.ToString(TagRenderMode.EndTag));

            Output.Write("<a href=\"#{0}\" class=\"left carousel-control\" data-slide=\"prev\"><span class=\"glyphicon glyphicon-chevron-left\"></span></a>", Id);
            Output.Write("<a href=\"#{0}\" class=\"right carousel-control\" data-slide=\"next\"> <span class=\"glyphicon glyphicon-chevron-right\"></span></a>", Id);

            Output.Write(outerDivTag.ToString(TagRenderMode.EndTag));

            Output.Write("<script>$(document).ready(function(){ $('" + Id + "').carousel();});</script>");

            //Output.Write("<script>$(function () {$('" + Id + "').carousel( );}); </script>");

        }


        static TagBuilder GetTagBuilder(string tagName, string id, IEnumerable<string> classes, IDictionary<string, string> attributes)
        {
            var tagBuilder = new TagBuilder(tagName);
            tagBuilder.MergeAttributes(attributes, false);
            foreach (var cssClass in classes ?? Enumerable.Empty<string>())
                tagBuilder.AddCssClass(cssClass);
            if (!string.IsNullOrWhiteSpace(id))
                tagBuilder.GenerateId(id);
            return tagBuilder;
        }

    }
}
